package com.example.leetcode;

/**
 * Created by Quincy on 2018/9/11.
 */
public class atoi {
    public int myAtoi(String str){
        if (str == null)
            return 0;
        str = str.trim();
        if (str.length() == 0)
            return 0;
        int sign = 1;
        int index = 0;

        if (str.charAt(index) == '+'){
            index++;
        }else if (str.charAt(index) == '-'){
            sign = -1;
            index++;
        }

        long num = 0;
        for ( ;index < str.length(); index++){
            if (str.charAt(index) < '0' && str.charAt(index) > '9')
                break;
            num = num*10 + (str.charAt(index) - '0');
            if (num > Integer.MAX_VALUE) break;
        }
        if (num * sign >= Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
        if (num * sign <= Integer.MIN_VALUE)
            return Integer.MIN_VALUE;
        return (int)num * sign;
    }
}
